package com.example.jpetstoreclient.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.jpetstoreclient.common.CommonResponse;
import com.example.jpetstoreclient.domain.Address;
import com.example.jpetstoreclient.persistance.AddressMapper;
import com.example.jpetstoreclient.service.AddressService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("addressService")
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {

    @Override
    public CommonResponse<Address> addAddress(int user_id,String address_content){
        Address address = new Address(user_id,address_content);
        if (save(address))return CommonResponse.createForSuccess("添加成功！",address);
        else return CommonResponse.createForError("添加失败！");
    }

    @Override
    public CommonResponse<Address> deleteAddress(int address_id){
        if(removeById(address_id))return CommonResponse.createForSuccessMessage("删除成功！");
        else return CommonResponse.createForError("删除失败！");
    }

    @Override
    public CommonResponse<Address> updateAddress(Address address){
        if(updateById(address))return CommonResponse.createForSuccess("修改成功！",address);
        else return CommonResponse.createForError("修改失败！");
    }

    @Override
    public CommonResponse<List<Address>> getAllAddresses(int user_id) {
        QueryWrapper<Address> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",user_id);

        List<Address> addresses = list(wrapper);
        return CommonResponse.createForSuccess("查询地址",addresses);
    }

}
